package top.v5it.japi.plus.core.spring;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.AuditorAware;
import top.v5it.japi.plus.common.context.Authorized;
import top.v5it.japi.plus.common.context.AuthorizedContextHolder;

import java.util.Optional;

/**
 * 审计
 *
 * @author zhanpu
 * @date 2020-11-9
 * @see org.springframework.data.domain.AuditorAware
 */
@Slf4j
public class JapiAuditorAware implements AuditorAware<String> {

    /**
     * 返回应用程序当前接入渠道号
     *
     * @return 接入渠道编号
     */
    @Override
    public Optional<String> getCurrentAuditor() {
        return Optional
                .ofNullable(AuthorizedContextHolder.getRequestAuthAttributes())
                .map(Authorized::getChannel);
    }
}
